<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul,
        ol {
            list-style: none;
        }

        .carousel {
            position: relative;
            width: 880px;
            height: 550px;
            border: 1px solid #333;
            margin: 50px auto;
        }

        .pic li {
            position: absolute;
            left: 0;
            top: 0;
            width: 880px;
            height: 550px;
            display: none;
        }

        .pic li.current {
            display: block;
        }

        .btn a {
            position: absolute;
            top: 50%;
            width: 80px;
            height: 80px;
            margin-top: -40px;
            background-color: rgba(255, 255, 255, 0.3);
            text-decoration: none;
            color: #444;
            text-align: center;
            line-height: 80px;
            font-size: 60px;
            font-family: "SimSun";
        }

        .btn .left {
            left: 10px;
        }

        .btn .right {
            right: 10px;
        }

        .btn a:hover {
            background-color: rgba(255, 255, 255, 0.7);
        }

        .sub {
            position: absolute;
            bottom: 30px;
            left: 50%;
            width: 200px;
            height: 40px;
            margin-left: -100px;
            border-radius: 20px;
            background-color: rgba(255, 255, 255, 0.3);
        }

        .sub li {
            float: left;
            width: 20px;
            height: 20px;
            margin: 10px;
            border-radius: 50%;
            background-color: #ccc;
            cursor: pointer;
        }

        .sub li.current {
            background-color: #0ff;
        }
    </style>
</head>

<body>
    <div class="carousel" id="carousel">
        <ul class="pic" id="pic">
            <li><img src="./images/01.jpg" alt=""></li>
            <li><img src="./images/02.jpg" alt=""></li>
            <li><img src="./images/03.jpg" alt=""></li>
            <li><img src="./images/04.jpg" alt=""></li>
            <li><img src="./images/05.jpg" alt=""></li>
        </ul>
        <div class="btn" id="btn">
            <a href="javascript:;" class="left" id="leftbtn">&lt;</a>
            <a href="javascript:;" class="right" id="rightbtn">&gt;</a>
        </div>
        <ol class="sub" id="sub">
        </ol>
    </div>
    <script>
        var idx = 0

        function dotted(ele, ele2) {
            for (var i = 0; i < ele.children.length; i++) {
                var olLis = document.createElement("li")
                ele2.appendChild(olLis)
            }
            ele2.children[idx].className = "current"
            ele.children[idx].className = "current"
        }

        function dotdefault() {
            for (var k = 0; k < pic.children.length; k++) {
                pic.children[k].className = ""
                sub.children[k].className = ""
            }
        }

        function idxEnd() {
            if (idx >= pic.children.length || idx < 0) {
                idx = idx >= pic.children.length ? 0 : (pic.children.length - 1)
            }
        }
        function pro() {
            idxEnd()
            dotdefault()
            pic.children[idx].className = "current"
            sub.children[idx].className = "current"
        }

        function leftmove() {
            idx++
            pro()
        }

        var carousel = document.getElementById("carousel")
        var pic = document.getElementById("pic")
        var sub = document.getElementById("sub")
        var btn1 = document.getElementById("leftbtn")
        var btn2 = document.getElementById("rightbtn")

        var timer

        timer = setInterval(leftmove, 2000)

        carousel.onmouseover = function () {
            clearInterval(timer)
            btn2.onclick = leftmove
            btn1.onclick = function () {
                idx--
                pro()

            }
            for (var i = 0; i < sub.children.length; i++) {
                sub.children[i].index = i
                sub.children[i].onclick = function () {
                idx = this.index
                pro()
            }
            }

        }

        carousel.onmouseout = function () {
            timer = setInterval(leftmove, 2000)
        }


        dotted(pic, sub)
        for (var i = 0; i < sub.children.length; i++) {
            sub.children[i].index = i
        }
    </script>

</body>

</html>